package com.woniu.service.Impl;

import com.alibaba.fastjson.JSON;
import com.woniu.dao.TestPaperDao;
import com.woniu.dao.TestQuestionDao;
import com.woniu.service.OperateBankService;
import com.woniu.service.OperatePaperService;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.enums.TestBankEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @program: bzeducation
 * @author: Xingqilin
 * @create: 2020-07-23 17:56
 * @description:
 */
@Service
public class OperatePaperServiceImpl implements OperatePaperService {

    @Resource
    TestPaperDao testPaperDao;
    @Resource
    TestQuestionDao testQuestionDao;
    @Resource
    OperateBankService operateBankService;

    @Override
    @Transactional
    public int insertPaper(TestPaper testPaper) {
        int i = 0;
//        将试卷基本信息写入试卷库，并获取ID
        i += testPaperDao.insertPaper(testPaper);
//        遍历试卷问题
        for (int index = 0; index < testPaper.getPaperQuestions().size(); index++) {
//            判断问题题型
            if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.SELECT_BANK.getIndex()) {
//                前端接受object类型会变成LinkedHashMap
//                通过转json转对象的方式变成对应题库对象
                SelectBank selectBank = JSON.parseObject(
//                        先将LinkedHashMap转为json
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , SelectBank.class);
//                将问题写入具体题库
                operateBankService.insertSelectBank(selectBank);
//                将选择题ID与试卷ID插入关联表
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(selectBank.getSelectId());
            }
//            其他题库与更新同理
            else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.VACANCY_BANK.getIndex()) {
                VacancyBank vacancyBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , VacancyBank.class);
                operateBankService.insertVacancyBank(vacancyBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(vacancyBank.getVacancyId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.MATERIAL_BANK.getIndex()) {
                MaterialBank materialBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , MaterialBank.class);
                operateBankService.insertMaterialBank(materialBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(materialBank.getMaterialId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.REVISE_BANK.getIndex()) {
                ReviseBank reviseBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , ReviseBank.class);
                operateBankService.insertReviseBank(reviseBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(reviseBank.getReviseId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.JUDGE_BANK.getIndex()) {
                JudgeBank judgeBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , JudgeBank.class);
                operateBankService.insertJudgeBank(judgeBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(judgeBank.getJudgeId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.CLOZE_BANK.getIndex()) {
                ClozeBank clozeBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , ClozeBank.class);
                operateBankService.insertClozeBank(clozeBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(clozeBank.getClozeId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.WRITER_BANK.getIndex()) {
                WriterBank writerBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , WriterBank.class);
                operateBankService.insertWriterBank(writerBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(writerBank.getWriterId());
            }
            testQuestionDao.insertTestQuestion(testPaper.getPaperQuestions().get(index));
        }
        return i;

    }

    @Override
    @Transactional
    public int updatePaper(TestPaper testPaper) {
        int i = 0;
//        将试卷基本信息写入试卷库，并获取ID
        i += testPaperDao.updatePaper(testPaper);
//        遍历试卷问题
        for (int index = 0; index < testPaper.getPaperQuestions().size(); index++) {
//            判断问题题型
            if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.SELECT_BANK.getIndex()) {
//                前端接受object类型会变成LinkedHashMap
//                通过转json转对象的方式变成对应题库对象
                SelectBank selectBank = JSON.parseObject(
//                        先将LinkedHashMap转为json
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , SelectBank.class);
//                将问题写入具体题库
                operateBankService.updateSelectBank(selectBank);
//                将选择题ID与试卷ID插入关联表
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(selectBank.getSelectId());
            }
//            其他题库与更新同理
            else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.VACANCY_BANK.getIndex()) {
                VacancyBank vacancyBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , VacancyBank.class);
                operateBankService.updateVacancyBank(vacancyBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(vacancyBank.getVacancyId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.MATERIAL_BANK.getIndex()) {
                MaterialBank materialBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , MaterialBank.class);
                operateBankService.updateMaterialBank(materialBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(materialBank.getMaterialId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.REVISE_BANK.getIndex()) {
                ReviseBank reviseBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , ReviseBank.class);
                operateBankService.updateReviseBank(reviseBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(reviseBank.getReviseId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.JUDGE_BANK.getIndex()) {
                JudgeBank judgeBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , JudgeBank.class);
                operateBankService.updateJudgeBank(judgeBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(judgeBank.getJudgeId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.CLOZE_BANK.getIndex()) {
                ClozeBank clozeBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , ClozeBank.class);
                operateBankService.updateClozeBank(clozeBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(clozeBank.getClozeId());
            } else if (testPaper.getPaperQuestions().get(index).getPaperQuestionBank()
                    == TestBankEnum.WRITER_BANK.getIndex()) {
                WriterBank writerBank = JSON.parseObject(
                        JSON.toJSONString(
                                testPaper.getPaperQuestions().get(index).getPaperDetailQuestion())
                        , WriterBank.class);
                operateBankService.updateWriterBank(writerBank);
                testPaper.getPaperQuestions().get(index).setPaperQuestionQid(writerBank.getWriterId());
            }
            testQuestionDao.updateTestQuestion(testPaper.getPaperQuestions().get(index));
        }
        return i;
    }

    @Override
    @Transactional
    public int deletePaper(Long paperId) {
        int i = 0;
        i += testPaperDao.deletePaper(paperId);
        i += testQuestionDao.deleteTestQuestion(paperId);
        return i;
    }

    @Override
    public long generatePaper(TestPaper testPaper) {
        testPaperDao.insertPaper(testPaper);
        return testPaper.getPaperId();
    }

    @Override
    public int insertQuestion(List<TestQuestion> testQuestions) {
        int i = 0;
        for (TestQuestion testQuestion : testQuestions) {
            i += testQuestionDao.insertTestQuestion(testQuestion);
        }
        return i;
    }
}
